import httpManager from './HttpManager.js';
import WxConfig from '../config/WxConfig.js';

/**
 * 发送code给后台换取用户微信信息
 * @param {*} param0
 */
const sendCode = ({
  tip = '微信登录中',
  code = '',
}) => {
  const request = {
    code: code,
    wechatAppId: WxConfig.appId
  }
  return httpManager.request({
    tip: tip,
    url: '/wechat/wechat-openid',
    data: request,
    method: httpManager.POST
  })
}


/**
 * 密码登录
 * @returns {Promise<*>}
 * @param phone     手机号
 * @param password  密码
 */
const loginPwd = (phone, password) => {
  const request = {
    username: phone,
    password: password
  };
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: '登录中',
      url: '/cas/jwt/login/user/token'
    });
};


/**
 * 登出
 */
const logout = () => {
  const request = {
  };
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: '登出中...',
      url: '/cas/jwt/user/logout'
    });
};
/**
 * 获取登录凭证
 */
const getCertificate = () => {
  const request = {
    app_code: "U2FsdGVkX19FWaEUgpvpmXkZ4ahA7KIJoYjw8NwlZsFo2ueNTbqZql1RgZzl1Hc"
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '登录中',
      url: '/certificate'
    });

}

/**
 * 获取当前用户信息
 */
const getCurrentUserInfo = () => {
  const request = {};
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '登录中',
      url: '/foodsupplychainService/ingredients/account/getCurrentUserInfo'
    });
}

/**
 * 获取验证码
 */
const getCaptcha = (phone) => {
  const request = {
    phone: phone
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '正在获取验证码',
      url: '/cas/sms/get/captcha'
    });
}

/**
 * 校验验证码
 */
const checkCaptcha = (phone, captcha) => {
  const request = {
    phone: phone,
    captcha: captcha
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '加载中...',
      url: '/cas/sms/check/captcha'
    });
}

/**
 * 找回密码
 */
const findPassword = (phone, captcha, password) => {
  const request = {
    phone: phone,
    captcha: captcha,
    password: password
  };
  return httpManager
    .request({
      method: httpManager.PUT,
      data: request,
      tip: '加载中...',
      url: '/userCenter/user/union/findPassword'
    });
}

/**
 * 修改密码
 */
const updatePassword = (captcha, password) => {
  const request = {
    captcha: captcha,
    password: password
  };
  return httpManager
    .request({
      method: httpManager.PUT,
      data: request,
      tip: '加载中...',
      url: '/userCenter/user/union/updatePassword'
    });
}

/**
 * 获取供应商详情
 */
const supplierInfo = (loginAccountId) => {
  const request = {
    loginAccountId: loginAccountId
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      url: '/foodsupplychainService/ingredients/app/supplier/info'
    });
}

/**
 * 查询司机列表
 */
const driverList = (seachObject) => {
  const request = seachObject;
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/app/driver/list'
    });
}

/**
 * 添加司机
 */
const addDriver = (driverObject) => {
  const request = driverObject;
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/app/driver/add'
    });
}

/**
 * 编辑司机
 */
const updateDriver = (driverObject) => {
  const request = driverObject;
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/app/driver/update'
    });
}

/**
 * 获取供应商订单详情
 */
const getOrderDettails = (supplierId, statusId,isTip) => {
  const request = {
    supplierId: supplierId,
    statusId: statusId
  };
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: isTip?'加载中...':"",
      url: '/foodsupplychainService/ingredients/app/detail/post'
    });
}


/**
 * 上传单个文件
 * @param filePath
 * @param tip
 * @returns {Promise<*>}
 */
const uploadFile = (filePath, otherData) => {
  return httpManager.uploadFile({
    url: '/resourceService/resource/common/indirectUpload',
    filePath: filePath,
    otherFormData: otherData
  })
};

/**
 * 查询供应商订单列表
 */
const orderList = (searchObject,isTip) => {
  const request = searchObject;
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: isTip?'加载中...':"",
      url: '/foodsupplychainService/ingredients/app/order/list'
    });
}

/**
 * 获取供应商的服务园所列表
 */
const cooperativeSchoolList = (supplierId) => {
  const request = {
    supplierId: supplierId
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/app/cooperativeSchool/list'
    });
}

/**
 * 机构或园所信息查看
 */
const organizationbindingInfo = (organizationId) => {
  const request = {
    organizationId: organizationId
  };
  return httpManager
    .request({  
      method: httpManager.GET,
      data: request,
      tip: '加载中...',
      url: '/organizationService/organization/getByOrganizationId'
    });
}

/**
 * 供应商食材品类列表
 */
const listOfCategory = (supplierId) => {
  const request = {
    supplierId: supplierId
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/supplier/listOfCategory'
    });
}

/**
 * 依据品类获取食材列表
 */
const productsList = (orgId, categoryId) => {
  const request = {
    orgId: orgId,
    categoryId: categoryId
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/products/list'
    });
}

/**
 * 获取食材详情
 */
const productsDetail = (productId) => {
  const request = {
    productId: productId
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/products/info'
    });
}

/**
 * 获取采购单详情
 */
const purchaseorderDetail = (id) => {
  const request = {
    id: id
  };
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/order/detail/post'
    });
}

/**
 * 批量更新订单状态 
 */
const updateOrderStatus = (searchObject) => {
  const request = searchObject;
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      url: '/foodsupplychainService/ingredients/order/status/post'
    });
}

/**
 * 园所列表
 */
const listOfSchool = (searchObject) => {
  const request = searchObject;
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/organizationbinding/listOfBindSchool'
    });
}

/**
 * 获取供应商待发货的服务园所列表
 */
const sendSchool = (searchObject) => {
  const request = searchObject;
  return httpManager
    .request({
      method: httpManager.POST,
      data: request,
      url: '/foodsupplychainService/ingredients/app/sendSchool/post'
    });
}



/**
 * 获取供应商备货单和确认订单的数量
 */
const getNum = (supplierId) => {
  const request = {
    supplierId: supplierId
  };
  return httpManager
    .request({
      method: httpManager.GET,
      data: request,
      url: '/foodsupplychainService/ingredients/app/num/get'
    });
}

/**
 * 获取供应商备货单和确认订单的数量
 */
const driverDelete = (driverId) => {
  const request = {
  };
  return httpManager
    .request({
      method: httpManager.DELETE,
      data: request,
      tip: '加载中...',
      url: '/foodsupplychainService/ingredients/app/driver/delete?driverId=' + driverId
    });
}

export default {
  sendCode,
  loginPwd,
  logout,
  getCertificate,
  getCurrentUserInfo,
  getCaptcha,
  checkCaptcha,
  findPassword,
  updatePassword,
  supplierInfo,
  driverList,
  addDriver,
  updateDriver,
  uploadFile,
  getOrderDettails,
  orderList,
  cooperativeSchoolList,
  organizationbindingInfo,
  listOfCategory,
  productsList,
  productsDetail,
  purchaseorderDetail,
  updateOrderStatus,
  listOfSchool,
  sendSchool,
  getNum,
  driverDelete
}